﻿namespace XSerialization.Tests

open NUnit.Framework
open System.Xml
open System.Xml.Serialization
open System.IO
open System
open System.Text
open System.Collections.Generic

type Internal () =
    member x.IntProp with get() = 2 and set(v : int) = ()
    member x.GuidProp with get() = new Guid("44df62d2-8fb5-43e1-a5d5-dee90130d508") and set(v : Guid) = ()

type Test(v) =
    new() = Test(0)
    member x.Str with get () = "hihihi" and set(v: string) = ()
    member x.Prop1 with  get () = v and set(v : int) = ()
    member x.Intern with  get () = new Internal() and set(v : Internal) = ()

type ListHolder () = 
    member x.List = new List<int>(seq { for i in 0..1 -> i })
    member x.Seq = seq { for i in 0..10 -> i }

type Pair<'T1, 'T2> (f : 'T1, s : 'T2) =
    member x.First : 'T1 = f
    member x.Second : 'T2 = s

[<TestFixture>]
type Fixture() =
    let inMemory f =
        let builder = new StringBuilder()
        let writer = XmlWriter.Create(builder)    
        f writer
        writer.Close()
        builder.ToString()

    let standart obj =
        let exec (writer : XmlWriter) =
            let serializer = new XmlSerializer(obj.GetType())
            let ns = new XmlSerializerNamespaces()
            ns.Add("", null)
            serializer.Serialize(writer, obj, ns)
        inMemory exec 

    let library obj = 
        let exec writer = XSerialization.serialize writer obj
        inMemory exec


    let assertData (o : obj) = Assert.AreEqual(standart o, library o)

    let simpleTest (o : obj) =
        let f w = XSerialization.serialize w o
        let result = inMemory f
        Assert.IsNotEmpty(result)
        

    [<Test>]
    member this.SomeObjectStructureTest () = assertData (new Test(5))

    [<Test>]
    member this.IEnumerableSimpleListTest () = 
        let data = List<int>(seq { for i in 0..2 -> i })
        simpleTest data
        
    [<Test>]
    member this.ComplexGenericTypeTest () = 
        let data = Pair(seq { for i in 0..2 -> Pair(i, i.ToString()) }, seq { for i in 0..2 -> i.ToString() })
        simpleTest data

    [<Test>]
    member this.IDictionaryTest () = 
        let data = new Dictionary<string, int>()
        data.Add("asdf", 1)
        data.Add("qwer", 2)
        simpleTest data 

    [<Test>]
    member this.GenericStructureTest () = 
        let data = new KeyValuePair<int, string>(12, "")
        simpleTest data 
